from utils.handle_excel_xlwings import ExcelHandle
from Common.basePage import BasePage

class cdPage(BasePage):
    def to_cd_page(self):
        self.click_element(self.ceshi_button)
        self.click_element(self.ceshidan_button)

class testtaskPage(BasePage):
    def to_testtask_page(self,dic):
        self.click_element(self.currentItem_button)
        self.input_text(self.currentItem_textbox,dic["Directory"])
        self.operate_enter()
        if dic["Csdshunxu"] == '1':
            self.click_element(self.ceshidan_case1_button)
        elif dic["Csdshunxu"] == '2':
            self.click_element(self.ceshidan_case2_button)
        elif dic["Csdshunxu"] == '3':
            self.click_element(self.ceshidan_case3_button)
        elif dic["Csdshunxu"] == '4':
            self.click_element(self.ceshidan_case4_button)

class testcasePage(BasePage):
    def to_testcase_page(self,data1,dic,rangexlsm,rangexlsmafter):
        """
        不同类中，调用

        Args:
            data1 (_type_): _description_
            dic (_type_): _description_
            rangexlsm (_type_): _description_
            rangexlsmafter (_type_): _description_
        """
        self.click_element(self.bysearchTab_button)
        self.click_element(self.field1_chosen_button)
        self.input_text(self.field1_chosen_optionbox,'用例编号')
        self.operate_enter()
        if len(data1) == 5: 
            Bugtitle, Bugcontent, Severity, Casecode, dymcappoint = data1
            self.input_text(self.field1_result_button,int(Casecode))
            # time.sleep(1)
            self.click_element(self.case_search_button)
            if self.get_element_text(self.cur_result) != '失败':
                self.click_element(self.cur_case)
                self.click_element(self.operation_button)
                self.click_element(self.fail_option)
                self.click_element(self.save_button)
            # time.sleep(1)
            if self.get_element_text(self.cur_bug_num) == '0':
                self.click_element(self.tobug_button)
                self.switch_to_frame(self.tobug_popup_frame)
                self.operate_pagedown()
                # time.sleep(1)
                self.click_element(self.tobug_button_popup)
                # self.switch_newest_window(4)
                self.switch_newest_window()
                self.click_element(self.curappoint_button)
                # time.sleep(1)
                
                if dymcappoint == 'occupy':
                    self.input_text(self.curappoint_textbox,dic["Curappoint"])
                    self.operate_enter()
                    if dic["Curappoint"] not in self.get_element_text(self.curappoint_result):
                        self.click_element(self.curappoint_button)
                        self.input_text(self.curappoint_textbox,dic["Curappoint"])
                        self.operate_enter()
                else:
                    self.input_text(self.curappoint_textbox,dymcappoint)
                    self.operate_enter()
                    if dymcappoint not in self.get_element_text(self.curappoint_result):
                        self.click_element(self.curappoint_button)
                        self.input_text(self.curappoint_textbox,dymcappoint)
                        self.operate_enter()
                self.input_text(self.bugtitle_textbox,Bugtitle,dic["BugtitleAppend"])
                # 设置输入追加标志填1时，前面加逗号，填入bug标题；设置输入追加标志填0时，清空内容，填入bug标题；
                severity_choices = {
                    1: (self.severity_choice1, self.priority_choice1),
                    2: (self.severity_choice2, self.priority_choice2),
                    4: (self.severity_choice4, self.priority_choice4),
                }
                if Severity in severity_choices:
                    self.click_element(self.severity_button)
                    self.click_element(severity_choices[Severity][0])
                    self.click_element(self.priority_button)
                    self.click_element(severity_choices[Severity][1])
                self.switch_to_frame(self.frame_to_reprosteps)
                self.input_text(self.article_content,Bugcontent)
                self.frame_to_default()
                self.operate_pagedown()
                self.operate_pagedown()
                self.click_element(self.bugsubmit_button)
                self.close_cur_window()
                self.switch_newest_window()
                self.click_element(self.tobug_close_button_popup)
            dyrange = ['D'+str(rangexlsm+2)+rangexlsmafter]
            ExcelHandle('casetobug.xlsm').wirte_range(dyrange)    
        elif isinstance(data1,list) and all(isinstance(item, list) and len(item) == 5 for item in data1):
            for i in range(len(data1)):
                Bugtitle = data1[i][0]
                Bugcontent = data1[i][1]
                Severity = data1[i][2]
                Casecode = int(data1[i][3])
                dymcappoint = data1[i][4]
                self.input_text(self.field1_result_button,Casecode)
                # time.sleep(1)
                self.click_element(self.case_search_button)
                if self.get_element_text(self.cur_result) != '失败':
                    self.click_element(self.cur_case)
                    self.click_element(self.operation_button)
                    self.click_element(self.fail_option)
                    self.click_element(self.save_button)
                # time.sleep(1)
                if self.get_element_text(self.cur_bug_num) == '0':
                    self.click_element(self.tobug_button)
                    self.switch_to_frame(self.tobug_popup_frame)
                    self.operate_pagedown()
                    # time.sleep(1)
                    self.click_element(self.tobug_button_popup)
                    # self.switch_newest_window(4)
                    self.switch_newest_window()
                    self.click_element(self.curappoint_button)
                    # time.sleep(1)
                    
                    if dymcappoint == 'occupy':
                        self.input_text(self.curappoint_textbox,dic["Curappoint"])
                        self.operate_enter()
                        if dic["Curappoint"] not in self.get_element_text(self.curappoint_result):
                            self.click_element(self.curappoint_button)
                            self.input_text(self.curappoint_textbox,dic["Curappoint"])
                            self.operate_enter()
                    else:
                        self.input_text(self.curappoint_textbox,dymcappoint)
                        self.operate_enter()
                        if dymcappoint not in self.get_element_text(self.curappoint_result):
                            self.click_element(self.curappoint_button)
                            self.input_text(self.curappoint_textbox,dymcappoint)
                            self.operate_enter()
                    self.input_text(self.bugtitle_textbox,Bugtitle,dic["BugtitleAppend"])
                    # 设置输入追加标志填1时，前面加逗号，填入bug标题；设置输入追加标志填0时，清空内容，填入bug标题；
                    severity_choices = {
                        1: (self.severity_choice1, self.priority_choice1),
                        2: (self.severity_choice2, self.priority_choice2),
                        4: (self.severity_choice4, self.priority_choice4),
                    }
                    if Severity in severity_choices:
                        self.click_element(self.severity_button)
                        self.click_element(severity_choices[Severity][0])
                        self.click_element(self.priority_button)
                        self.click_element(severity_choices[Severity][1])
                    self.switch_to_frame(self.frame_to_reprosteps)
                    self.input_text(self.article_content,Bugcontent)
                    self.frame_to_default()
                    self.operate_pagedown()
                    self.operate_pagedown()
                    self.click_element(self.bugsubmit_button)
                    self.close_cur_window()
                    self.switch_newest_window()
                    self.click_element(self.tobug_close_button_popup)
                dyrange = ['D'+str(i+rangexlsm+2)+rangexlsmafter]
                ExcelHandle('casetobug.xlsm').wirte_range(dyrange)
    def oper_importbug(self, data1,dic,rangexlsm,rangexlsmafter):
        cdPage().to_cd_page()
        testtaskPage().to_testtask_page(dic)
        testcasePage().to_testcase_page(data1,dic,rangexlsm,rangexlsmafter)


if __name__ == '__main__':
    from loginPageToChandao import xnPage
    from utils.handle_excel_xlwings import ExcelHandle
    dic = ExcelHandle('casetobug.xlsm').get_excel_area_data('B2:B7')
    data1,rangexlsm,rangexlsmafter= ExcelHandle('casetobug.xlsm').get_excel_case_data()
    print(data1)
    print(len(data1))
    print(int(data1[3]))